package com.xiaotu.spider.weibo.pageprocessor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.Connection.Response;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.StarHotModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.GsonUtils;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;

/**
 * @类名 StarWeiboHotPageProcessor
 * @日期 2017年4月25日
 * @作者 高海军
 * @功能 艺人微博热搜数据爬取
 */
@Component("StarWeiboHotPageProcessor")
public class StarWeiboHotPageProcessor extends AUrlPageProcessor
{
	
	private static final String[] START_URL = {"http://www.wrd.cn/view/home/hotPeople/getHotPeopleRank.action?labels=19,208&time=1"}; 
	
	//豆瓣搜索地址
	private static final String DOUBAN_SEARCH_URL = "https://movie.douban.com/celebrities/search?search_text=%s";
	
	private static final String TEMP_URL = "https://www.baidu.com/item/%s";//用于创建page
	
	@Override
	public String getPageDataType(Page page)
	{
		String url = page.getRequest().getUrl();
		if (url.contains("getHotPeopleRank.action"))//微博热搜前20名
            return "WEIBO_SEARCH_LIST";
		if (url.contains("https://www.baidu"))
			return "BAIDU";
        return StringUtils.EMPTY;
		
	}
	
	@Override
	public void process(Page page)
	{
		Map<String,Object> extraMap = page.getRequest().getExtras();
        String type = this.getPageDataType(page);
        switch (type)
        {
            case "WEIBO_SEARCH_LIST":
                this.weiBoSearchlistProcess(page);
                break;
            case "BAIDU":
            	this.douBanStarInfoProcess(page,extraMap);
                break;
        }
	}
	


	/** listProcess(微博热搜前20名演员)   
	 * 创建人：张晓   
	 * 创建时间：2020年1月6日 下午5:07:29    
	 * 修改备注： 
	 * @param page  
	 */
	@SuppressWarnings("unchecked")
	private void weiBoSearchlistProcess(Page page) {
		String json = page.getRawText();
		if (StringUtils.isEmpty(json))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
		Map<String,Object> jsonMap = GsonUtils.fromJson(json, Map.class);
		List<Map<String,Object>> list = (List<Map<String,Object>>)jsonMap.get("list");
		if (null == list || list.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
		for(Map<String,Object> map : list){
			String name = map.get("name") == null?null:map.get("name").toString();
			String labelName = map.get("name") == null?null:map.get("labelName").toString().replace(" ", "/");
			if(StringUtils.isNotBlank(name)){
				Map<String,Object> everyExtraMap = new HashMap<String,Object>();
				everyExtraMap.put("name", name);
				everyExtraMap.put("labelName", labelName);
				//豆瓣搜索列表
				douBanSearchlistProcess(page,everyExtraMap);
			}
		}
	}

	/** douBanSearchlistProcess(豆瓣搜索列表)   
	 * 创建人：张晓   
	 * 创建时间：2020年1月6日 下午5:45:11    
	 * 修改备注： 
	 * @param page  
	 */
	private void douBanSearchlistProcess(Page page,Map<String,Object> extraMap) {
		String weiBo_StarName = extraMap.get("name") == null?null:extraMap.get("name").toString();
		String weiBo_LabelName = extraMap.get("labelName") == null?null:extraMap.get("labelName").toString();
		Connection conn = connent(String.format(DOUBAN_SEARCH_URL, KeywordEncode.encode(weiBo_StarName)));
		try {
			conn.get();
			Response rs = conn.response();
			String html = rs.body();
			if(StringUtil.isNotEmpty(html)){
				Document documentHtml = Jsoup.parse(html,"utf8");
				Elements aTag = documentHtml.select(".result .pic").select("a");
				Elements ulTag = documentHtml.select(".result .content").select("ul");
				List<String> douBan_labelNameList = new ArrayList<String>();
				ulTag.stream().forEach(u -> {
					douBan_labelNameList.add(u.select("li").get(0).text());
				});
				
				if(null!=aTag && null!= douBan_labelNameList && aTag.size() == douBan_labelNameList.size()){
					for(int i=0;i<aTag.size();i++){
						org.jsoup.nodes.Element aTagElment = aTag.get(i);
						String href = aTagElment.attr("href");
						String douBan_StarName = RegexUtils.regexChStr(aTagElment.select("img").attr("alt")).replaceAll("[/ ,]", "/");
						String douBan_LabelName = douBan_labelNameList.get(i).replace(" ", "").replaceAll("[/ ,]", "/");
						if(StringUtils.isNotBlank(douBan_StarName) && StringUtils.isNotBlank(weiBo_StarName)
								&& (isContains(weiBo_StarName,douBan_StarName) || isContains(douBan_StarName,weiBo_StarName))){
							if(StringUtils.isNotBlank(weiBo_LabelName) && StringUtils.isNotBlank(douBan_LabelName)
									&& (isContains(douBan_LabelName,weiBo_LabelName) || isContains(weiBo_LabelName,douBan_LabelName))){
								//正确匹配之后获取演员性别
								Map<String,Object> everyExtraMap = new HashMap<String,Object>();
								everyExtraMap.put("weiBo_StarName", weiBo_StarName);
								everyExtraMap.put("weiBo_LabelName", weiBo_LabelName);
								everyExtraMap.put("url", href);
								PageUtils.addTargetRequest(page, String.format(TEMP_URL, KeywordEncode.encode(weiBo_StarName)), everyExtraMap);
							}
						}
						
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	/** douBanStarInfoProcess(豆瓣艺人基本信息)   
	 * 创建人：张晓   
	 * 创建时间：2020年1月6日 下午5:45:13    
	 * 修改备注： 
	 * @param page  
	 */
	private void douBanStarInfoProcess(Page page,Map<String,Object> extraMap) {
		
		Connection conn = connent(extraMap.get("url").toString());
		try {
			conn.get();
			Response rs = conn.response();
			String html = rs.body();
			if(StringUtil.isNotEmpty(html)){
				Document documentHtml = Jsoup.parse(html,"utf8");
				Elements liTag = documentHtml.select("#headline .info").select("ul").select("li");
				liTag.stream().anyMatch(li -> {
					boolean flag = false;
					String title = li.select("span").text();
					if(title.contains("性别")){
						String sex = RegexUtils.regexChStr(li.text().substring(li.text().lastIndexOf(":")+1));
						StarHotModel starHotModel = new StarHotModel(extraMap.get("weiBo_StarName").toString(), sex, Constants.SiteName.SINA);
						page.putField(StarHotModel.class.getName(), starHotModel);
						flag = true;
					}
					return flag;
				});
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected String[] getStartRequestUrls()
	{
		return START_URL;
	}
	
	
	
	@Override
	protected String getSpiderType()
	{
		return Constants.SpiderTaskType.STAR_WEIBO_HOT;
	}
	
	//链接目标url
  	public static Connection connent(String url) {
      	Connection conn = Jsoup.connect(url)    
                  .userAgent("Mozilla/5.0 (Windows NT 6.1; rv:47.0) Gecko/20100101 Firefox/47.0") // http请求的浏览器设置
                  .timeout(5000)   // http连接时长
                  .maxBodySize(0)
                  .method(Connection.Method.GET);
      	return conn;
    }	
	
  	private boolean isContains(String str1,String str2){
		
		boolean flag = false;
		String[] str1Arr = str1.split("/");
		String[] str2Arr = str2.split("/");
		for(String s1 : str1Arr){
			for(String s2 : str2Arr){
				if(s1.contains(s2)){
					flag = true;
					return flag;
				}
			}
		}
		
		return flag;
	}
  	
}
